﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace NetworkSupervisor
{
    public partial class FormRegister : DevExpress.XtraEditors.XtraForm
    {
        private int regTryCount = 4;

        public FormRegister()
        {
            InitializeComponent();
        }

        private void textEdit2_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private SayakaHelper.RegisterHelper reg = null;

        private void FormRegister_Load(object sender, EventArgs e)
        {
            Icon = Properties.Resources.app;
            string registerCodePath = Properties.Resources.RegisterCodePath;
            string registerCodeKey = Properties.Resources.RegisterCodeKey;
            reg = new SayakaHelper.RegisterHelper(registerCodePath, registerCodeKey);
            txtMachineCode.Text = reg.MachineCode;
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            string registerCode = txtRegisterCode.Text;
            if (reg.CheckRegisterCode(registerCode))
            {
                reg.SetRegisterCodeInRegedit(registerCode);
                XtraMessageBox.Show(string.Format("注册成功。您的注册码为：{0}。请牢记。", registerCode));
                DialogResult = DialogResult.OK;
            }
            else
            {
                regTryCount--;
                if (regTryCount == 0)
                {
                    XtraMessageBox.Show(string.Format("您已连续4次注册失败，请联系程序作者。"));
                    DialogResult = DialogResult.Cancel;
                }
                else
                {
                    XtraMessageBox.Show(string.Format("注册失败。您还有{0}次机会。", regTryCount));
                }
            }
        }

        private void FormRegister_Shown(object sender, EventArgs e)
        {
            txtRegisterCode.Focus();
        }
    }
}